package cn.openea.perpartner.meily.exception;

import cn.openea.perpartner.common.core.exception.LockTimeoutException;
import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 处理rest接口异常的处理器
 */
@RestControllerAdvice(annotations = RestController.class)
@Slf4j
public class MeilyRestExceptionHandler {

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public AjaxResult dataValidationException(MethodArgumentNotValidException e) {
        log.error("数据校验出现异常:{},异常类型为:{}", e.getMessage(), e.getClass());
        return AjaxResult.error("参数格式校验失败");
    }

    @ExceptionHandler(value = LockTimeoutException.class)
    public AjaxResult lockTimeoutExceptionException(LockTimeoutException e) {
        log.error("redisSession 锁超时");
        return AjaxResult.error(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(value = ServiceException.class)
    public AjaxResult mallServiceException(ServiceException e) {
        log.error("异常信息为:{},异常类型为:{}", e.getMessage(), e.getClass());
        e.printStackTrace();
        return AjaxResult.error(e.getCode() == null ? 500 : e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public AjaxResult unknownException(Exception e) {
        log.error("异常信息为:{},异常类型为:{}", e.getMessage(), e.getClass());
        e.printStackTrace();
        return AjaxResult.error("未知错误，请重试");
    }

}
